package com.example.springbootdemo2024.controller;

import com.example.springbootdemo2024.dao.RoomDao;
import com.example.springbootdemo2024.entity.Room;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomDao roomDao;
    /**
     * 普通用户操作——预订房间
     */
    @RequestMapping("/bookRoom")
    @ResponseBody
    public void bookRoom(HttpServletRequest request, Integer id){
        HttpSession session = request.getSession();//生成session对象
        Integer userid = (Integer) session.getAttribute("userid");//获取用户id
        roomDao.bookRoom(id);
        roomDao.connUser(id, userid);
    }

    /**
     * 获取空闲房间数
     */
    @RequestMapping("/getEmptyCount")
    @ResponseBody
    public int getEmptyCount(){
        return roomDao.getEmptyCount();
    }

    /**
     * 获取空闲房间
     */
    @RequestMapping("/findEmptyAll")
    @ResponseBody
    List<Room> findEmptyAll(Integer limit, Integer page){
        List<Room> rooms = roomDao.findEmptyAll(limit, limit*(page-1));
        return rooms;
    }
}
